package com.cskaoyan.controller.mall;/*-*- coding = utf-8 -*-
@Time : 2022/11/19 16:13
@Author : 林涂兵
@File : OrderController.java
@Software: IntelliJ IDEA*/


import com.cskaoyan.bean.bo.OrderReqParaBo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.mall.OrderDataVo;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/order")
public class OrderController {
    // 自建的用来查询所有订单
    @Autowired
    MarketOrderListLimit marketOrderListLimit;

    // 订单商品
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    // 订单
    @Autowired
    MarketOrderMapper marketOrderMapper;

    // 用户
    @Autowired
    UserMapper userMapper;

    // marketChannel
    @Autowired
    MarketChannelMapper marketChannelMapper;

    // 评论
    @Autowired
    MarketCommentMapper marketCommentMapper;
    /**
     *
     * @param baseParam 接收到的用于排序的参数 当前页 每页的size 排序方式
     * @return 所有的订单信息
     */
    @RequestMapping("list")
    public BaseRespVo getOrderLimit(OrderReqParaBo baseParam) throws ParseException {
        // 插入快递公司信息
        // insertChannel();

        // 将sort变量转换成对应的排序形式
        // 将字符串转换为date类型
        SimpleDateFormat formatter  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date end = null;
        Date start = null;
        System.out.println(baseParam.getEnd());
        if (!StringUtils.isEmpty(baseParam.getStart())  && !StringUtils.isEmpty(baseParam.getEnd())){
            end = formatter.parse(baseParam.getEnd());
            start = formatter.parse(baseParam.getStart());
        }
        // 去除空格
        if (!StringUtils.isEmpty(baseParam.getOrderSn())){
            baseParam.setOrderSn(baseParam.getOrderSn().trim());
        }
        baseParam.setSort("addTime");

        // 进行分页操作 参数是 当前页 和 每页的size
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        // 根据条件获取所有的订单信息
        ArrayList<MarketOrder> allOrderLimit = marketOrderListLimit.getAllOrderLimit(baseParam,end,start);
        // 建立返回的对象
        CommonData<MarketOrder> marketOrderCommonData = new CommonData<>();
        marketOrderCommonData.setList(allOrderLimit);
        marketOrderCommonData.setLimit(baseParam.getLimit());
        marketOrderCommonData.setPage(baseParam.getPage());

        // 获取分页后的信息
        PageInfo<MarketOrder> marketOrderPageInfo = new PageInfo<>(allOrderLimit);
        marketOrderCommonData.setTotal(Integer.valueOf((int) marketOrderPageInfo.getTotal()));
        marketOrderCommonData.setPages(marketOrderPageInfo.getPages());

        // 最终返回的对象
        BaseRespVo<CommonData> marketOrderBaseRespVo = new BaseRespVo<>();
        marketOrderBaseRespVo.setErrno(0);
        marketOrderBaseRespVo.setErrmsg("成功");
        marketOrderBaseRespVo.setData(marketOrderCommonData);
        return marketOrderBaseRespVo;
    }

    /**
     * 通过请求参数中的id 获取订单详情，商品详情，用户详情
     * @param id 待查询的订单id
     * @return 订单详情，商品详情
     */
    @RequestMapping("detail")
    public BaseRespVo getOrderDetail(@RequestParam("id") Integer id){
        // 通过订单id，获取对应的订单
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        // 通过订单id，获取对应的商品
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByOrderId(id);
        // 判空
        if (marketOrder == null){
            return BaseRespVo.succeed();
        }
        Integer userId = marketOrder.getUserId();
        // 通过订单id，获取对应user
        MarketUser user  = userMapper.selectById(userId);

        // 创建返回的对象
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        OrderDataVo orderDataVo = new OrderDataVo(marketOrderGoods, user, marketOrder);

        baseRespVo.setData(orderDataVo);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);

        return baseRespVo;
    }

    /**
     *获取快递公司的信息
     */
    @RequestMapping("channel")
    public BaseRespVo getChannel(){
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        List<MarketChannel> marketChannels = marketChannelMapper.selectAllMarketChannel();
        baseRespVo.setData(marketChannels);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    /**
     *  根据传入的信息更新订单状态
     * @param map 订单id
     * 快递公司英文简称
     *  快递公司编号
     * @return
     */
    @RequestMapping("ship")
    @Transactional
    public  BaseRespVo updateShip(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");


        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(Integer.valueOf(orderId));
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);
        marketOrder.setUpdateTime(new Date());
        Short status = 301;
        marketOrder.setOrderStatus(status);

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

        return BaseRespVo.succeed();
    }

    // 根据订单id删除对应的订单
    @RequestMapping("delete")
    public BaseRespVo deleteOrder(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");

        marketOrderMapper.deleteByPrimaryKey(orderId);
        return BaseRespVo.succeed();
    }

    // 退款
    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        BigDecimal refundMoney = BigDecimal.valueOf((Double) map.get("refundMoney"));

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        Short status = 203;
        marketOrder.setOrderStatus(status);
        marketOrder.setRefundAmount(refundMoney);
        marketOrder.setRefundTime(new Date());
        marketOrder.setRefundType("微信退款接口");

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

        return BaseRespVo.succeed();

    }

    // 商品评论
    @RequestMapping("reply")
    public BaseRespVo replyComment(@RequestBody Map map){
        Integer commentId = (Integer) map.get("commentId");
        String content = (String) map.get("content");
        MarketComment marketComment = new MarketComment();
        marketComment.setId(commentId);
        marketComment.setContent(content);
        marketComment.setUpdateTime(new Date());
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);

        return BaseRespVo.succeed();
    }

}
